<template>
  <div>
    <!-- 
          el-dialog
          center	是否对头部和底部采用居中布局	boolean
       -->
    <el-dialog
      center
      :title="isAdd ? '添加菜单' : '编辑菜单'"
      :visible.sync="isShow"
      :before-close="cancel"
      :close-on-press-escape="false"
      
    >
      <el-form :model="menu_info" ref="menuInfo" :rules="rules">
        <el-form-item label="菜单名称" :label-width="formLabelWidth" prop="title">
          <el-input v-model="menu_info.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" :label-width="formLabelWidth" prop="pid">
          <el-select
            v-model="menu_info.pid"
            placeholder="请选择"
            @change="changePid"
          >
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in menulist"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio disabled v-model="menu_info.type" :label="1">目录</el-radio>
          <el-radio disabled v-model="menu_info.type" :label="2">菜单</el-radio>
        </el-form-item>
         <el-form-item  label="菜单图标" 
           v-if="menu_info.type == 1"
         :label-width="formLabelWidth">
          <el-select v-model="menu_info.icon" placeholder="请选择">
            <el-option
              v-for="item in iconlist"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
        
          v-else
          label="菜单地址"
          :label-width="formLabelWidth"
        >
          <el-select v-model="menu_info.url" placeholder="请选择">
            <el-option
              v-for="item in indexRoutes"
              :key="item.path"
              :label="item.meta.path"
              :value="item.path"
            >
            </el-option>
          </el-select>
        </el-form-item>
       
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="menu_info.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { indexRoutes } from "../../router/index";
import { menuAdd, menuInfo, menuEdit } from "../../request/api";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
     rules: {
        title: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
        pid: [{ required: true, message: "请选择上级菜单", trigger: "change" }],
      },
      menu_info: {
        title: "", //菜单名称
        pid: 0, //上级菜单编号
        type: 1, //类型 1目录2菜单
        url: "", //菜单地址
        icon: "", //菜单图标
        status: 1, //状态   1正常2禁用   number类型
      },
      formLabelWidth: "120px",
      options: [
        {
          value: 1,
          label: "系统管理",
        },
        {
          value: 2,
          label: "商城管理",
        },
      ],
      iconlist: [
        "el-icon-user-solid",
        "el-icon-warning",
        "el-icon-picture",
        "el-icon-s-comment",
      ],
      indexRoutes: indexRoutes,
    };
  },
  computed: {
    ...mapGetters({
      menulist: "menu/getmenus",
    }),
  },

  mounted() {
    this.getMenu();
  },
  methods: {
    ...mapActions({
      getMenu: "menu/getMenuList",
    }),
    // 清空表单
    reset() {
      this.menu_info = {
        title: "", //菜单名称
        pid: 0, //上级菜单编号
        type: 1, //类型 1目录2菜单
        url: "", //菜单地址
        icon: "", //菜单图标
        status: 1, //状态   1正常2禁用   number类型
      };
      this.$refs.menuInfo.resetFields();
    },
    //封装一个取消事件
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    changePid() {
      this.menu_info.type = this.menu_info.pid == 0 ? 1 : 2;
    },
    // 封装一个事件
    lookup(id) {
      console.log(id, 111111111111111);
      menuInfo({ id: id }).then((res) => {
        console.log(res);
        this.menu_info = res.list;
        this.menu_info.id=id
        console.log(this.menu_info)
      });
    },
    add() {
      console.log(this.menu_info);
      this.$refs.menuInfo.validate((val) => {
        if (val) {
          if (this.isAdd) {
            // c菜单添加
            menuAdd(this.menu_info).then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.getMenu();
              } else {
                this.$message.error(res.msg);
              }
              this.cancel();
            });
          } else {
            // 菜单编辑
            console.log(this.menu_info)
            menuEdit(this.menu_info).then((res) => {
              console.log(res);
               if (res.code == 200) {
                this.$message.success(res.msg);
                this.getMenu();
              } else {
                this.$message.error(res.msg);
              }
              this.cancel();
            });
          }
        }
      });
    },
  },
};
</script>

<style scoped></style>
